﻿<template>
    <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1000"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="采购单位名称" prop="purchaseUnityName">
                        <el-input v-model="entityObj.purchaseUnityName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="采购单位全称" prop="purchaseUnityFullName">
                        <el-input v-model="entityObj.purchaseUnityFullName" />
                    </el-form-item>
                </el-col>
               
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系人" prop="realName">
                        <el-input v-model="entityObj.realName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系电话" prop="mobile">
                        <el-input v-model="entityObj.mobile" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="联系地址" prop="address">
                        <el-input v-model="entityObj.address" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="是否自动接单" prop="isAutomaticReceive">
                        <el-switch
                            v-model="entityObj.isAutomaticReceive"
                            :active-value="true"
                            :inactive-value="false"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="制单人" prop="crOrderId">
                        <el-select
                            placeholder="---请选择制单人---"
                            v-model="entityObj.crOrderId"
                            style="width: 300px"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsAccount"
                                :key="item.accountId"
                                :label="item.realName"
                                :value="item.accountId"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="对账人" prop="reconciliationId">
                        <el-select
                            placeholder="---请选择对账人---"
                            v-model="entityObj.reconciliationId"
                            style="width: 300px"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsAccount"
                                :key="item.accountId"
                                :label="item.realName"
                                :value="item.accountId"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="客服姓名">
                        <el-input v-model="entityObj.customerServiceName" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="客服电话">
                        <el-input v-model="entityObj.customerServicePhone" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="客服微信">
                        <el-input v-model="entityObj.customerServiceWeChat" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="客服QQ">
                        <el-input v-model="entityObj.customerServiceQQ" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="金蝶编码">
                        <el-input v-model="entityObj.jdPurchaseUnityCode" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="金蝶仓库编码">
                        <el-input v-model="entityObj.jdWarehouseCode" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="标签">
                        <el-input v-model="entityObj.purchaseUnityTag" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="状态" prop="status">
                        <el-switch
                            v-model="entityObj.status"
                            :active-value="1"
                            :inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                status: 1,
            },
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            //验证规则
            rules: {
                purchaseUnityName: [
                    {
                        required: true,
                        type: "string",
                        message: "请填写门店名称",
                    },
                ],
                purchaseUnityFullName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                realName: [
                    { required: true, type: "string", message: "请填写" },
                ],
                mobile: [{ required: true, type: "string", message: "请填写" }],
                address: [
                    { required: true, type: "string", message: "请填写" },
                ],
                crOrderId:[{ required: true, type: "number", message: "请填写" }],
                reconciliationId:[{ required: true, type: "number", message: "请填写" }],
                status:[{ required: true, type: "number", message: "请填写" }],
                isAutomaticReceive:[{ required: true, type: "Boolean", message: "请填写" }], 
            },
            optionsStoreType: [],
            optionsPaymentType: [],
            optionsInvoicingType: [],
            areaTree:[],
            purchaseDept:[],
            optionsBrand:[],
            optionsAccount:[]
        };
    },
    methods: {
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    const data={ 
                        ...this.entityObj, 
                        brandName:this.optionsBrand.find(p=>p.value==this.entityObj.brandCode)?.label
                    }
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "purchase-unity/create"
                                    : "purchase-unity/update",data
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = {
                ...data,
                creditDay: Number(data.creditDay),
                areaCode:data.cityCode
            }; 
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    "account/options",
                    { }
                );
                if (returnValue.status == 200) {
					this.optionsAccount=returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        await this.initOptions();
    },
};
</script>

<style></style>
